package cs2340.g21.wms;

import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

/**
 * Displays a list of notes 
 * @author Alexander Dodson
 * @version 1.0
 *
 */
public class NoteListActivity extends Activity {

    private Coordinator coordinator;
    private static final String LOG_TAG = "debugger";
    private ListView listview;
    private ArrayList<Note> notes;
     
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        coordinator = new Coordinator();

        if(getIntent().getExtras() != null){
            coordinator = (Coordinator) getIntent().getExtras().getSerializable("cs2340.g21.wms.coordinator");
            if(getIntent().hasExtra("notes")){
                Log.d(LOG_TAG, "using provided list");
                notes = (ArrayList<Note>)getIntent().getExtras().get("notes");
            }
        }
        setContentView(R.layout.activity_note_list);
        
        //make a new adapter and set it as the "content creator" for listview
        Note[] temp = new Note[notes.size()];
        int x = 0;
        for(Note n : notes){
            temp[x++] = n;
        }
        NoteAdapter adapter = new NoteAdapter(this, R.layout.listview_note_row, temp);
        listview = (ListView)findViewById(R.id.notelistView);
        View header = (View)getLayoutInflater().inflate(R.layout.listview_noteheader_row, null);
        listview.addHeaderView(header);
        listview.setAdapter(adapter);
        listview.setOnItemClickListener(new OnItemClickListener() {
        	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        		Note clickedNote = (Note) listview.getItemAtPosition(position);
        		Intent intent = new Intent();
        		intent.putExtra("note", clickedNote);
        		intent.putExtra("notes", notes);
        		intent.setClass(NoteListActivity.this, NotePageActivity.class);
        		NoteListActivity.this.onPause();
        		startActivity(intent);
        		NoteListActivity.this.onResume();
        	}
        } );
    }
    
    /**
     * Method called by the back button
     * @param view
     */
    public void back(View view){
        Intent intent = new Intent();
        intent.putExtra("cs2340.g21.wms.coordinator", coordinator);
        intent.setClass(NoteListActivity.this, HomeScreenActivity.class);
        if(coordinator.getUser().isAdmin()){
            intent.setClass(NoteListActivity.this, AdminControlsActivity.class);
        }
        startActivity(intent);
        finish();
    }
 
    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        intent.putExtra("cs2340.g21.wms.coordinator", coordinator);
        intent.setClass(NoteListActivity.this, HomeScreenActivity.class);
        if(coordinator.getUser().isAdmin()){
            intent.setClass(NoteListActivity.this, AdminControlsActivity.class);
        }
        startActivity(intent);
        finish();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_user_item_list, menu);
        return true;
    }

}
